package com.assets.query;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.assets.databinding.QueryItemBinding;
import com.assets.room.database.MyDataBase;
import com.assets.util.RequestResult;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * @作者(author): WR
 * @创建时间(date): 2022/1/4
 */
public class QueryAdapter extends RecyclerView.Adapter<QueryAdapter.QueryViewHolder> {
    private List<QueryData> dataList;

    public QueryAdapter(List<QueryData> dataList) {
        this.dataList = dataList;
    }

    @NonNull
    @Override
    public QueryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        QueryItemBinding binding = QueryItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new QueryViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull QueryViewHolder holder, int position) {
        holder.bindData(dataList.get(position).getAsset());
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    private RequestResult<Integer> onClickListen;

    public void setOnClickListen(RequestResult<Integer> onClickListen) {
        this.onClickListen = onClickListen;
    }

    public class QueryViewHolder extends RecyclerView.ViewHolder {
        private QueryItemBinding mBinding;

        public QueryViewHolder(@NonNull QueryItemBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
            mBinding.getRoot().setOnClickListener(v -> {
                if (onClickListen != null) {
                    onClickListen.result(getAdapterPosition());
                }
            });
        }

        public void bindData(QueryData.AssetBean bean) {
//            if (!TextUtils.isEmpty(bean.getImage())) {
//                Glide.with(mBinding.AssetImg).load(bean.getImage()).into(mBinding.AssetImg);
//            }

            mBinding.tvCategory.setText(bean.getType());
            mBinding.tvName.setText(bean.getAssetName());


            if (bean.getAccountant().isEmpty() || "/".equals(bean.getAccountant())) {

            }
//            mBinding.tvManager.setText(bean.getDeptId());

            mBinding.tvManager.setText(bean.getCompany());


            mBinding.tvNo.setText(bean.getAssetNumber());
            mBinding.tvUser.setText(bean.getUserId());
            mBinding.tvModel.setText(bean.getModel());

            switch (bean.getStatus()) {
                case "2":
                    mBinding.tvStatus.setText("报废");
                    break;
                case "1":
                    mBinding.tvStatus.setText("在用");
                    break;
                case "4":
                    mBinding.tvStatus.setText("闲置");
                    break;
                case "10":
                    mBinding.tvStatus.setText("待打印");
                    break;
                default:
                    mBinding.tvStatus.setText(bean.getStatus());
                    break;
            }


            mBinding.tvRemark.setText(bean.getRemark());
        }
    }
}
